// asynchronous progress/cancel support
define(
  ["require", "jquery", "bip.cache", "bip.base", "bip.async"],
  function(require) {
    "use strict";

    var $ = require("jquery");
    var $bip = require("bip.base");
    
    var options = {
    	title:"编辑对话框",
    	modal:false,
    	width:600,
    	height:600,
    	templateData:null,
    	templateName:null,
    	templateHtml:null,		//对于需要在模板中绑定及时事件，如：自动提示，请传入模板最终的html内容
    	validateCallback:null,
    	confirmCallback:null,
    	argData:null
    };
    
    var notRemoveDialogDiv = {};
    var replaceSpecialExpr = /[\.#\/]/g;
    
    var mainDivID = "";
    
    function openBipDialog(content, op){
    	var mainDiv = "";
    	mainDiv += "<div id=\"" + mainDivID + "\"";
    	mainDiv += " title=\"" + op.title + "\"></div>";
    	$("body").append(mainDiv);
    	
    	
    	$("#" + mainDivID).empty().append(content);
    	
    	$('#' + mainDivID).dialog({
				    		close:function(){
				    			$(this).dialog("destroy");
				    			$('#' + mainDivID).remove();
				    		},
				    		autoOpen: true,
				    		position:{
				    			my:"center top",
				    			at:"center top",
				    			of:$("body")
				    		},
				    		modal:(op.modal ? true : false),
				    		width: op.width,
				            height: op.height,
				    		buttons: {
				    			'确定': function() {
				    				var $obj = $('#' + mainDivID);
				    				
				    				if(!op.validateCallback(null, $obj, op.argData)){
				    					return;
				    				}
				    				
				    				$(this).dialog('close');
				    				
				    				op.confirmCallback.call(null, $obj, op.argData);
				    			},
				    			'取消':function(){
				    				$(this).dialog('close');
				    			}
				    		}
				    	});
    }
    
    function extSelectedAttr(data, selected, isName){
    	$.each(data, function(index, item){
    		if(isName){
    			if(item.name === selected){
    				$.extend(item, {"selected": selected});
    			}
    		}else{
    			if(item.value === selected){
    				$.extend(item, {"selected": selected});
    			}
    		}
    	});
    }

    var $async = require("bip.async");
    
    function openEditorDialog(op){
    	if(!op.templateName || !op.templateData){
    		$bip.showMsg("模板名称或者模板数据不能为空!");
    		return;
    	}
    	
    	op = $.extend({}, options, op);
    	
    	mainDivID = op.templateName.replace(replaceSpecialExpr, "-");
    	
    	var dialogDivID = op.templateName;
    	
    	if(op.templateHtml === null){
    		//模板不绑定及时事件，可以调用该方法
    		$.when(
                    $async.asyncGetTmpl(op.templateName)
                  )
                 .done(function(tmplRes){               
                	 var html = $.templates[op.templateName].render(op.templateData);
                	 openBipDialog(html, op);
                 });
    	}else{
    		//模板内容绑定及时事件，比如：自动提示，则不能采用异步方式获取模板
    		openBipDialog(op.templateHtml, op);
    	}
    	
    	return mainDivID;
    }
    
    var bipDialog = {
    	openEditorDialog : openEditorDialog,
    	extSelectedAttr:extSelectedAttr
    };

    return bipDialog;
  }
);

